package com.googlecode.wheres_my_stuff_okay_potato;

import static android.widget.Toast.LENGTH_LONG;
import static android.widget.Toast.makeText;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * Class used to represent the buttons, GUI and functionality of adding a found
 * item.
 * 
 */
public class AddFoundItemActivity extends Activity {

	/**
	 * Creates the found item and performs functionality of submit button.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_found_item);

		final Global GLOBAL = (Global) getApplication();

		final EditText name_et = (EditText) findViewById(R.id.add_found_item_name_et);
		final EditText desc_et = (EditText) findViewById(R.id.add_found_item_desc_et);
		final EditText loc_et = (EditText) findViewById(R.id.add_found_item_loc_et);
		final Spinner category_sp = (Spinner) findViewById(R.id.add_found_item_category_spinner);

		findViewById(R.id.add_found_item_cancel_b).setOnClickListener(
				new OnClickListener() {
					public void onClick(View v) {
						Intent i = new Intent(
								getApplication().getBaseContext(),
								FoundItemListActivity.class);
						startActivity(i);
					}
				});

		findViewById(R.id.add_found_item_submit_b).setOnClickListener(
				new OnClickListener() {
					public void onClick(View v) {
						// First validate the name:
						String name = name_et.getText().toString();

						if (!name.matches("[A-Za-z\\d_\\-\\s]+")) {
							makeText(getApplication().getBaseContext(),
									R.string.password_format_toast, LENGTH_LONG)
									.show();
							name_et.requestFocus();
							return;
						}

						// The description doesn't really need to be validated.
						String desc = desc_et.getText().toString();
						// Check location
						String loc = loc_et.getText().toString();

						if (!loc.matches("[A-Za-z\\d_\\-\\s]+")) {
							makeText(getApplication().getBaseContext(),
									R.string.password_format_toast, LENGTH_LONG)
									.show();
							loc_et.requestFocus();
							return;
						}

						String categoryString = (String) category_sp
								.getSelectedItem();

						if (categoryString == null)
							categoryString = "Other";

						GLOBAL.addFoundItem(name, desc, loc, categoryString);

						Intent i = new Intent(
								getApplication().getBaseContext(),
								FoundItemListActivity.class);
						startActivity(i);
						finish();

						/**
						 * Match found code. Finds matches and then toasts.
						 */
						final Global GLOBAL = (Global) getApplication();
						List<Item> lost_items = GLOBAL.getFoundItems();

						for (Item item : lost_items) { // compare to all the
														// elements in the found
														// item list.
							if (item.getName().equals(name)
									&& item.getLoc().equals(loc)) {// if you
																	// find a
																	// match,
																	// present
																	// it to the
																	// user in
																	// the
																	// match_found
																	// activity.
								Toast.makeText(getApplicationContext(),
										"Match found!", Toast.LENGTH_LONG)
										.show();
							}
						}
					}
				});

	}

	/**
	 * Inflate the menu; this adds items to the action bar if it is present.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.create_item, menu);
		return true;
	}
}